package com.example.zhinongbao.activity;




import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhinongbao.R;
import com.example.zhinongbao.adapter.FootprintAdapter;
import com.example.zhinongbao.manager.ProductManager;
import com.example.zhinongbao.model.Product;
import com.example.zhinongbao.provider.ZhinongbaoContract;

import java.util.ArrayList;
import java.util.List;

public class CollectionActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private FootprintAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_footprint);

        // 配置工具栏
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("收藏");

        // 初始化RecyclerView
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        // 加载足迹数据
        loadFootprintData();
    }

    private void loadFootprintData() {
        // 查询数据库获取浏览记录
        Cursor cursor = getContentResolver().query(
                ZhinongbaoContract.FootprintEntry.CONTENT_URI,
                null,
                null,
                null,
                ZhinongbaoContract.FootprintEntry.COLUMN_TIMESTAMP + " DESC"
        );

        // 获取商品管理类实例
        ProductManager productManager = ProductManager.getInstance(getApplicationContext());

        List<Product> footprintProducts = new ArrayList<>();
        if (cursor != null) {
            while (cursor.moveToNext()) {
                int productId = cursor.getInt(cursor.getColumnIndexOrThrow(
                        ZhinongbaoContract.FootprintEntry.COLUMN_PRODUCT_ID));

                // 从商品管理器获取实际商品对象
                Product actualProduct = productManager.getProductById(productId);
                if (actualProduct != null) {
                    footprintProducts.add(actualProduct);
                }
            }
            cursor.close();
        }

        // 如果足迹为空，添加一些演示数据（从首页商品选取）
        if (footprintProducts.isEmpty()) {
            // 获取所有商品列表
            List<Product> allProducts = productManager.getAllProducts();

            // 添加一些实际商品作为演示
            if (!allProducts.isEmpty()) {
                footprintProducts.add(allProducts.get(0)); // 第一个商品
                if (allProducts.size() > 1) {
                    footprintProducts.add(allProducts.get(1)); // 第二个商品
                }
            }
        }
        // 设置适配器
        adapter = new FootprintAdapter(footprintProducts);
        recyclerView.setAdapter(adapter);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

